#!/bin/bash
print_info_ret()
{
    echo "processor_name: " $1       
    echo "test_name:      " $2       
    echo "model_name:     " $3       
    echo "image_name:     " "ImageNet"
    echo "batch size:     " $4       
    echo "latency:        " $8 "ms"
    echo "throughout(1pcs)" $5 "fps"      
    echo "top1:           " $6      
    echo "top5:           " $7    
}

function echo_title {
    echo "***************************************************************************************************************************"
    echo $1
    echo "***************************************************************************************************************************"
}

# print log function
function echo_log {
    case $1 in
        0)
            # green
            echo -e "[success]\033[32m $2\033[0m"
            ;;
        1)
            # red
            echo -e "[error]\033[31m $2\033[0m"
            ;;
        2)
            # yellow
            echo -e "\033[33m$2\033[0m"
            ;;
        *)
            echo -e "[info] $2"
            ;;
    esac
}

# check cmd run result
function check {
    if [ $1 -eq 0 ]
    then
        echo_log 0 "$2"
    else
        echo_log 1 "$3"
        exit 1
    fi
}

# Set environment variables
chmod +x ../Script/env.sh
source ../Script/env.sh

# run Atc
input_shape="input:1,224,224,3" # N H W C
commod="atc --model=./resnet50.pb --input_shape=$input_shape --input_format=NHWC --framework=3 --insert_op_conf=./resnet50_aipp.cfg --output=./resnet50_tf_bs1_fp16 --soc_version=Ascend310"

echo_title "$commod"
$commod
check $? "atc run success" "atc run failed"

# Get OM file path
path=$PWD
ommode=$path"/resnet50_tf_bs1_fp16.om"

# run benchmark
bcmk_path="../"
cd $bcmk_path
chmod +x benchmark
run_cmd="./benchmark -model_type=vision -batch_size=1 -device_id=0 -om_path=$ommode -input_text_path=./Script/ImageNet.info -input_width=224 -input_height=224 -useDvpp=true"
echo ''
echo_log 2 "-------------Benchmark start working now, please wait for a moment---------------. "
echo_title "$run_cmd"
sleep 4s
$run_cmd
ok=$?
check $ok "benchmark run success, welcome to the next use." "benchmark run fsiled"

echo ""
#Post-processing
echo_log 2 "Post-processing start working now, please wait for a moment."
cd Script
python3 vision_metric.py ../result/dumpOutput_device0/ ./imagenet1024/HiAIAnnotations ./ result.json
echo "=======================result info============================"
acc="`python3.7 parse.py result.json ../result/perf_vision_batchsize_1_device_0.txt`"
arr=(${acc//,/ })
print_info_ret "Ascend310" "classification" "resnet50" "1" ${arr[2]} ${arr[0]} ${arr[1]} ${arr[3]}
echo "=============================================================="


